package com.dt76.smallloanbackendservice.utils;

import com.dt76.smallloanbackendservice.mapper.etao.SLogMapper;
import com.dt76.smallloanbackendservice.pojo.etao.CurrentUser;
import com.dt76.smallloanbackendservice.pojo.etao.SLog;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component("aopLogger")
@Aspect
public class AopLogger {

    //注入日志dao接口
    @Autowired
    private SLogMapper sLogMapper;

    //配置查询增强
//    @Around("execution(* com.dt76.smallloanbackendservice.service.*.impl.*.get*(..))")
//    public Object aroundSelect(ProceedingJoinPoint point) {
//        Object result = new Object();
//        try {
//            result = point.proceed();
//            //增删改类型判断:根据方法名前3个字母去判断
//        } catch (Throwable throwable) {
//            sLogMapper.insert(new SLog(1, throwable.getMessage()));
//        }
//        return result;
//    }

    //配置新增
    @Around("execution(* com.dt76.smallloanbackendservice.service.*.impl.*.add*(..))")
    public Object aroundInsert(ProceedingJoinPoint point) {
        System.out.println("进入添加日志=============================");
        Object result = new Object();
        try {
            result = common(1, point);
        } catch (Throwable throwable) {
            sLogMapper.insert(new SLog(1, throwable.getMessage()));
        }
        return result;
    }
    //配置修改
    @Around("execution(* com.dt76.smallloanbackendservice.service.*.impl.*.upd*(..))")
    public Object aroundModify(ProceedingJoinPoint point) {
        System.out.println("进入修改日志=============================");
        Object result = new Object();
        try {
            result = common(2, point);
        } catch (Throwable throwable) {
            sLogMapper.insert(new SLog(1, throwable.getMessage()));
        }
        return result;
    }
    //配置删除
    @Around("execution(* com.dt76.smallloanbackendservice.service.*.impl.*.del*(..))")
    public Object aroundRemove(ProceedingJoinPoint point) {
        System.out.println("进入删除日志=============================");
        Object result = new Object();
        try {
            result = common(3, point);
        } catch (Throwable throwable) {
            sLogMapper.insert(new SLog(1, throwable.getMessage()));
        }
        return result;
    }

    //公共方法
    private Object common(int type, ProceedingJoinPoint point) throws Throwable {
        Object result = new Object();
        result = point.proceed();
        //增删改类型判断:根据方法名前3个字母去判断
        SLog sLog = new SLog();
        StringBuilder columns = new StringBuilder();
        Object[] args = point.getArgs();
        for (Object arg : args) {//无数个参数
            System.out.println(arg.toString()+"===================================进入切面公共方法");
            if (arg instanceof CurrentUser) {//定位所有参数中类型为CurrentLoginUser的
                CurrentUser currentUser = (CurrentUser) arg;
                sLog.setCurrentUser(currentUser.getcEmp().geteId());//获取操作人
            } else if (arg instanceof Integer){
                sLog.setCurrentUser((Integer) arg);
            }
            columns.append(arg.toString());
        }
        sLog.setLogColumns(columns.toString());
        sLog.setLogType(type);//1代表新增操作

        sLogMapper.insert(sLog);

        return result;
    }
}
